package rz.string;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

public class StringLength {

	/**
	 * 生僻字,java编码
	 * @param args
	 */
	public static void main(String[] args) {
		String str = "a喆";//傢俬玥荘  手机中输入一个"喆",剩余字数减1
		System.out.println(str.length());//2
		try {
			byte[] unicodes = null;
			unicodes = "喆".getBytes("unicode");
			System.out.println("unicode " + unicodes.length);
			for (byte b : unicodes) { //输出的FFFE 则为Little-Endian BOM-"Byte Order Mark"
				System.out.print(b + " ");
			}
			System.out.println();
			
			unicodes = str.getBytes("unicode");
			System.out.println("unicode " + unicodes.length);
			for (byte b : unicodes) {
				System.out.print(b + " ");
			}
			System.out.println();
			
			unicodes = str.getBytes("UnicodeBigUnmarked");//由大到小 字节顺序
			System.out.println("UnicodeBigUnmarked " + unicodes.length);
			for (byte b : unicodes) {
				System.out.print(b + " ");
			}
			System.out.println();
			
			unicodes = str.getBytes("UnicodeLittleUnmarked");//由小到大 字节顺序
			System.out.println("UnicodeLittleUnmarked " + unicodes.length);
			for (byte b : unicodes) {
				System.out.print(b + " ");
			}
			System.out.println();
			
			unicodes = str.getBytes("UTF-8");//java 里边unicode到底用哪个实现的,utf-16?
			System.out.println("UTF-8 " + unicodes.length);
			for (byte b : unicodes) {
				System.out.print(b + " ");
			}
			System.out.println();

			unicodes = str.getBytes("GBK");
			System.out.println("GBK " + unicodes.length);
			for (byte b : unicodes) {
				System.out.print(b + " ");
			}
			System.out.println();
			
			unicodes = str.getBytes("ISO8859-1");
			System.out.println("ISO8859-1 " + unicodes.length);
			for (byte b : unicodes) {
				System.out.print(b + " ");
			}
			System.out.println();
			
			unicodes = str.getBytes();
			System.out.println(Charset.defaultCharset().name() + unicodes.length);
			for (byte b : unicodes) {
				System.out.print(b + " ");
			}
			System.out.println();
			
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

}
